def find_set(x):
    if x != s[x]:
        s[x] = find_set(s[x])  # 路径压缩
    return s[x]

def merge_set(x, y):
    x = find_set(x)
    y = find_set(y)
    if x != y:
        s[x] = s[y]

n, q = map(int, input().split())
s = list(range(n + 1))
a = list(map(int, input().split()))  # 0索引空出

# 初始化并查集
for i in range(1, n+1):
    if a[i-1] != 0:  # a数组索引从0开始对应元素1
        root = find_set(a[i-1])
        s[i] = root  # 合并到父节点

for _ in range(q):
    l, r = map(int, input().split())
    if l > r:
        continue
    # 找到区间的代表元素（这里选右端点）
    root = find_set(r)
    # 将区间内所有元素合并到代表元素
    for i in range(l, r+1):
        s[find_set(i)] = root

for i in range(1, n+1):
    find_set(i)
s_set = set(s)
print(len(s_set) - 1)